package com.store.app.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.MemberTradeRecordAdapter;
import com.store.app.bean.MemberTradRecordBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.ToastUtils;
import com.store.app.widget.AutoListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ben
 * on 2016/9/1 0001.\
 */
public class MembersTradeRecordActivity extends BaseActivity implements IOnHttpListener{
    private AutoListView mListView;
    private NetRequest netRequest;
    private int pageNum=1;
    private int pageSize=15;
    private ImageView public_ll_return;
    private TextView tvTitle;
    private MemberTradeRecordAdapter memberTradeRecordAdapter;
    private List<MemberTradRecordBean>beanList=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_members_trade_record);
        initView();

        mListView = (AutoListView) findViewById(R.id.lv_members);
        memberTradeRecordAdapter=new MemberTradeRecordAdapter(beanList,this);
        mListView.setAdapter(memberTradeRecordAdapter);

        netRequest = new NetRequest(this);

        mListView.setOnRefreshListener(new AutoListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNum = 1;
                if (!TextUtils.isEmpty(MainActivity.member_id)) {
                    netRequest.requestTradeRecord(2, pageNum + "", pageSize + "", MainActivity.member_id);
                }
            }
        });

        mListView.setOnLoadListener(new AutoListView.OnLoadListener() {

            @Override
            public void onLoad() {
                if (!TextUtils.isEmpty(MainActivity.member_id)) {
                    netRequest.requestTradeRecord(1, pageNum + "", pageSize + "", MainActivity.member_id);
                }
            }
        });
        mListView.onRefresh();

    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("详情");
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onAfinalFail() {
        mListView.onRefreshComplete();
        mListView.onLoadComplete();
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        //加载更多
        if (requestId == 1) {
            try {
                JSONObject data = new JSONObject(result);
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                List<MemberTradRecordBean> tempList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    MemberTradRecordBean openCardDetailBean = gson.fromJson(listArr.getJSONObject(i).toString(), MemberTradRecordBean.class);
                    tempList.add(openCardDetailBean);
                }
                int page_no = data.getJSONObject("page").getInt("page_no");
                mListView.onLoadComplete();
                if (page_no == pageNum) {
                    mListView.setResultSize(tempList.size());
                    beanList.addAll(tempList);
                    memberTradeRecordAdapter.notifyDataSetChanged();
                    pageNum += 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 2) {
            try {
                JSONObject data = new JSONObject(result);
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                beanList.clear();
                for (int i = 0; i < listArr.length(); i++) {
                    MemberTradRecordBean memberTradRecordBean = gson.fromJson(listArr.getJSONObject(i).toString(), MemberTradRecordBean.class);
                    beanList.add(memberTradRecordBean);
                }
                mListView.onRefreshComplete();
                mListView.setResultSize(beanList.size());
                memberTradeRecordAdapter.notifyDataSetChanged();
                pageNum += 1;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        mListView.onRefreshComplete();
        mListView.onLoadComplete();
        ToastUtils.showTextToast(MembersTradeRecordActivity.this,msg);
    }
}
